<template>
    <div id="new_home">
   		<ma-scroll :down="true" :getMore="getshield" :showMsg="false" :init="init">
                  <dynamic v-for="(item,index) in forkObj" :isShow="false" :obj="item" @shield="shield" :key="index" :index="index"></dynamic>
   		            <empty v-if="!forkObj || forkObj.length === 0" message="暂无关注"></empty>
   		        </ma-scroll>
    </div>
</template>
<script>
import dynamic from "components/dynamic/dynamic_home";
import getLocation from "services/get-location";
import defaultImg from "assets/flagImg.png";
import empty from "components/empty-page";
import scroll from "components/loadmore";
//services
import recommendServices from "services/dynamicService/dynamic_recommended";
//wx
import wx from "weixin-js-sdk";

export default {
  name: "service_fork_list",
  components: {
    dynamic,
    empty,
    scroll
  },
  data() {
    return {
      latitude: "43.821877",
      longitude: "125.239955",
      forkObj: [],
      defaultImg: defaultImg,
      shieldIndex: -5,
      recommendIndex: -5,
      count: "5",
      init: false,
      shieldAllLoaded: false,
      isWxPublic: false
    };
  },
  created() {
    if (this.$.env === "wxPublic") {
      this.isWxPublic = true;
    }
    this.getshield().then(res => {
      this.init = true;
    });
  },
  mounted() {
    if (this.$.env === "webOrWechat") {
      document.getElementById("wrapper").classList = "wxCss";
    }
  },
  methods: {
    //计算距离
    getDistance(objs) {
      console.log(objs);
      for (let i = 0; i < objs.length; i++) {
        //      let date = obj[i].publishDate;
        //      obj[i].publishDate = date.substring(5, 11);
        let distance = getLocation.getFlatternDistance(
          objs[i].shop.latitude,
          objs[i].shop.longitude,
          this.latitude,
          this.longitude
        );
        distance = Math.ceil(distance); // 转换后的距离值
        objs[i].distance =
          distance > 1000
            ? (distance / 1000).toFixed(1) + "km"
            : distance < 100 ? " 100m " : distance + "m";
      }
      return objs;
    },
    //获取关注列表
    getshield() {
      let vm = this;
      return new Promise(function(resolve, reject) {
        vm.shieldIndex += 5;
        recommendServices.bind(vm)(
          {
            type: "",
            index: vm.shieldIndex,
            count: vm.count
          },
          res => {
            console.log(res);
            vm.forkObj = vm.forkObj.concat(res);
            let objs = vm.getDistance(res);
            console.log(objs);
            resolve();
          },
          err => {},
          {
            type: "fork"
          }
        );
      });
    },
    //屏蔽信息
    shield(shield, index) {
      shieldService.bind(this)(
        {
          id: shield.id
        },
        res => {
          this.recommendObj.splice(index, 1);
          this.$.toast("屏蔽信息成功");
        },
        err => {}
      );
    }
  }
};
</script>
<style>
#new_home .scroller {
  width: 100vw;
}
#new_home #wrapper {
  top: 100px;
}
.wxCss {
  top: 50px !important;
}
</style>
